package com.example.stock;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class InputDialog extends AlertDialog implements OnClickListener {
    private EditText etnum,etname,etprice;  //编辑框
    private Button btnConfrim, btnCancel;  //确定取消按钮
    private TextView vtitle;
    private String tsinfo;
    private OnEditInputFinishedListener mListener; //接口

    public interface OnEditInputFinishedListener{
        void editInputFinished(String recom,String price,String num,String buyfee);
    }

    protected InputDialog(Context context, OnEditInputFinishedListener mListener,String title) {
        super(context);
        this.mListener = mListener;
        tsinfo=title;//实际用于存name
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addorless);

        //控件
        etname = (EditText)findViewById(R.id.name);
        etname.setText(tsinfo);
        etprice = (EditText)findViewById(R.id.price);
        etnum = (EditText)findViewById(R.id.num);
        btnConfrim = (Button)findViewById(R.id.btn_confirm);
        btnCancel = (Button)findViewById(R.id.btn_cancel);
        //vtitle = (TextView)findViewById(R.id.textView4);
        //vtitle.setText(tsinfo);

        btnConfrim.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_confirm) {
            //确定
            if (mListener != null) {
                String name = etname.getText().toString().replace(" ", "");
                int num=etnum.getText().length()>0?Integer.parseInt(etnum.getText().toString()):0;
                float price=Float.parseFloat(etprice.getText().toString());
                float buyfee=price*num*0.00013>5.0f?price*num*0.00013f:5.0f;
                buyfee=Math.round(buyfee*100)/100.0f;
                if(num>0)mListener.editInputFinished(name,etprice.getText().toString(),etnum.getText().toString(),Float.toString(buyfee));
            }
            dismiss();
        }else {
            //取消
            dismiss();
        }
    }

}
